package de.windeln.marketing.validation;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.google.common.base.Strings;

import de.windeln.marketing.campaign.model.RequestClientObject;

@Component
public class VariableInformationByGivenNumberOfVariable implements Validator{

	@Override
	public boolean supports(Class<?> clazz) {
		return clazz == RequestClientObject.class;
	}

	@Override
	public void validate(Object target, Errors errors) {
		RequestClientObject rco = (RequestClientObject) target;
		if(rco.getNumberOfVariable() != 0){
			if(rco.getCostPerVariable() == 0.0){
				errors.rejectValue("numberOfVariable", "Variable Information", "By given field 'Number of Variable' fields 'Cost per Variable' and 'Variable' must be filled in, too!");
				errors.rejectValue("costPerVariable", "Variable Information", "By given field 'Number of Variable' fields 'Cost per Variable' and 'Variable' must be filled in, too!");
			}
			if(Strings.isNullOrEmpty(rco.getVariable())){
				errors.rejectValue("numberOfVariable", "Variable Information", "By given field 'Number of Variable' fields 'Cost per Variable' and 'Variable' must be filled in, too!");
				errors.rejectValue("variable", "Variable Information", "By given field 'Number of Variable' fields 'Cost per Variable' and 'Variable' must be filled in, too!");
			}
		}
		
		if(rco.getCostPerVariable() != 0.0){
			if(Strings.isNullOrEmpty(rco.getVariable())){
				errors.rejectValue("variable", "Variable Information", "By given field 'Cost per Variable' field 'Variable' must be filled in, too!");

			}
		}
	
	}

}
